package com.hl.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.Company;
import com.hl.sfzy.entity.course.Course;
import com.hl.sfzy.service.CompanyService;
import com.hl.sfzy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;


@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;
//    根据id查询某个公司信息
    @GetMapping("/getAbout")
    public R company(int id){
        Company company = companyService.company(id);
        System.out.println(company);
        if (company != null){
            return R.success(company);
        }else {
            return R.failure("查询失败");
        }
    }
//    查询全部公司信息
    @GetMapping("/all")
    public R<PageInfo<Company>> companyAllContent(Integer pageNo, Integer pageSize) {
        PageInfo<Company> companyPageInfo = companyService.companyAllContent(pageNo, pageSize);
        return R.success(companyPageInfo);
    }

//    模糊查询
    @GetMapping("/like")
    public R<PageInfo<Company>> courseLikeSearch(Integer pageNo, Integer pageSize, String name) {
        PageInfo<Company> coursePageInfo = companyService.companyLikeSearch(pageNo, pageSize,name);
        return R.success(coursePageInfo);
    }

    //查看图片
    @GetMapping("/downImg")
    public void down(String file, HttpServletResponse resp) throws Exception {
        String path = "d:\\picture\\" + file;
        FileInputStream inputStream = new FileInputStream(path);
        ServletOutputStream outputStream = resp.getOutputStream();

        byte[] bytes = new byte[1024];
        int i = inputStream.read(bytes);
        while (i != -1) {
            outputStream.write(bytes, 0, i);
            i = inputStream.read(bytes);
        }
        outputStream.close();
        inputStream.close();

    }
    //下载图片到路径
    @PostMapping("/fileup")
    public R<String> upload(MultipartFile file) throws IOException {
        String path ="d:\\picture\\";
        File file1 = new File(path);
        if (!file1.exists()){
            file1.mkdir();
        }
        String originalFilename = file.getOriginalFilename();
        String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
        String filename = UUID.randomUUID().toString() + ext;
        path = path + filename;
        file.transferTo(new File(path));
        return R.success(filename);
    }

    //新增课程
    @PostMapping("/add")
    public R<Company> companyInsert(Company company){
        int i = companyService.companyInsert(company);
        return i>0?R.success(company):R.failure("500");
    }

    //删除课程
    @DeleteMapping("/delete")
    public R<Integer> companyDelete(Long id){
        int i= companyService.companyDelete(id);
        return R.success(i);
    }

    //修改课程
    @PutMapping("/update")
    public R<Company> companyUpdate(Company company){

        System.out.println(company);

        int i = companyService.updateCompany(company);
        return R.success(company);
    }
}
